package commons.logs;

import java.util.ArrayList;
import javax.swing.JFrame;

public class Logger {

	private static boolean bKeepLogInRAM;
	private static ArrayList<String> vLogs;
	private static ArrayList<Level> vLevels;
	
	public static void keepLogInRAM() {
		bKeepLogInRAM = true;
		vLogs = new ArrayList<>();
		vLevels = new ArrayList<>();
	}
	public static void resetLogInRAM() {
		vLogs = new ArrayList<>();
		vLevels = new ArrayList<>();
	}
	
	public static enum Level {
		DEBUG2(99), DEBUG1(90), INFO2(70), INFO1(50), WARN_(20), ERROR(10);
		public int val;
		private Level(int v) {val=v;}
		//public String GetLevelFromName
	}

	static private Level level=null;

	static {
		Logger.level = Level.DEBUG2; // initialement, le plus verbeux, mais le niveau souhaité par l'utilisateur va etre renseigné dès la lecture des paramètres
	}

	static public void setOutLevel (Level level) {Logger.level = level;}
	static public Level getOutLevel () {return Logger.level;}
	static public boolean isDebugLevel() {return Logger.level.val >= Level.DEBUG1.val;}
	

	static public void log (Level level, String msg) {
		if (Logger.level.val >= level.val) {
			if (level.val >= Level.DEBUG1.val)
				doLog(level, "~~ DEBUG : " + msg);
			else if (level==Level.WARN_)
				doLog(level, "!! WARNING !! " + msg);
			else if (level==Level.ERROR)
				doLog(level, "!!## ERROR ##!! " + msg);
			else
				doLog(level, msg);
		}
	}
	
	static private void doLog (Level level, String fullMsg) {
		if (bKeepLogInRAM) {
			vLevels.add(level);
			vLogs.add(fullMsg);
		}
		System.out.println(fullMsg);
	}

	static public void showLogInRAM(JFrame frame) {
		if (!bKeepLogInRAM)
			return;
		DlgLogs dlg = new DlgLogs(frame, true, vLogs, vLevels);
		dlg.setVisible(true);
	}
	
	static public void log_DEBUG2 (String msg) {log(Level.DEBUG2, msg);}
	static public void log_DEBUG1 (String msg) {log(Level.DEBUG1, msg);}
	static public void log_INFO2 (String msg) {log(Level.INFO2, msg);}
	static public void log_INFO1 (String msg) {log(Level.INFO1, msg);}
	static public void log_WARN (String msg) {log(Level.WARN_, msg);}
	static public void log_ERROR (String msg) {log(Level.ERROR, msg);}
}
